package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EnergyConsumptionDTO;
import com.ruoyi.system.domain.EnergyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotCubeElectricityConsumptionHistoryMapper;
import com.ruoyi.system.domain.IotCubeElectricityConsumptionHistory;
import com.ruoyi.system.service.IIotCubeElectricityConsumptionHistoryService;

/**
 * IoT数据_六面控温用电量记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class IotCubeElectricityConsumptionHistoryServiceImpl extends ServiceImpl<IotCubeElectricityConsumptionHistoryMapper,IotCubeElectricityConsumptionHistory> implements IIotCubeElectricityConsumptionHistoryService
{
    @Autowired
    private IotCubeElectricityConsumptionHistoryMapper iotCubeElectricityConsumptionHistoryMapper;

    /**
     * 查询IoT数据_六面控温用电量记录
     * 
     * @param id IoT数据_六面控温用电量记录主键
     * @return IoT数据_六面控温用电量记录
     */
    @Override
    public IotCubeElectricityConsumptionHistory selectIotCubeElectricityConsumptionHistoryById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询IoT数据_六面控温用电量记录列表
     * 
     * @param iotCubeElectricityConsumptionHistory IoT数据_六面控温用电量记录
     * @return IoT数据_六面控温用电量记录
     */
    @Override
    public List<IotCubeElectricityConsumptionHistory> selectIotCubeElectricityConsumptionHistoryList(IotCubeElectricityConsumptionHistory iotCubeElectricityConsumptionHistory)
    {
        return iotCubeElectricityConsumptionHistoryMapper.selectIotCubeElectricityConsumptionHistoryList(iotCubeElectricityConsumptionHistory);
    }

    /**
     * 新增IoT数据_六面控温用电量记录
     * 
     * @param iotCubeElectricityConsumptionHistory IoT数据_六面控温用电量记录
     * @return 结果
     */
    @Override
    public boolean insertIotCubeElectricityConsumptionHistory(IotCubeElectricityConsumptionHistory iotCubeElectricityConsumptionHistory)
    {
        return this.save(iotCubeElectricityConsumptionHistory);
    }

    /**
     * 修改IoT数据_六面控温用电量记录
     * 
     * @param iotCubeElectricityConsumptionHistory IoT数据_六面控温用电量记录
     * @return 结果
     */
    @Override
    public boolean updateIotCubeElectricityConsumptionHistory(IotCubeElectricityConsumptionHistory iotCubeElectricityConsumptionHistory)
    {
        return this.updateById(iotCubeElectricityConsumptionHistory);
    }

    /**
     * 批量删除IoT数据_六面控温用电量记录
     * 
     * @param ids 需要删除的IoT数据_六面控温用电量记录主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubeElectricityConsumptionHistoryByIds(Long[] ids)
    {
        return super.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除IoT数据_六面控温用电量记录信息
     * 
     * @param id IoT数据_六面控温用电量记录主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubeElectricityConsumptionHistoryById(Long id)
    {
        return this.removeById(id);
    }

    @Override
    public EnergyConsumptionDTO getTotalEnergyConsumption(IotCubeElectricityConsumptionHistory query) {
        EnergyConsumptionDTO result = new EnergyConsumptionDTO();
        List<EnergyDTO> list = new ArrayList<>();

        List<IotCubeElectricityConsumptionHistory> resultList = baseMapper.getList(query);
        handleData(list, resultList, query.getType());
        result.setList(list);
        //查询每日最大值
        List<IotCubeElectricityConsumptionHistory> maxList = baseMapper.getDailyMax(query);
        List<BigDecimal> vauleList = new ArrayList<>();
        handleMaxData(maxList, vauleList, query.getType());
        if (CollectionUtil.isNotEmpty(vauleList)) {
            result.setMax(Collections.max(vauleList));
            result.setMin(Collections.min(vauleList));
            BigDecimal sum = vauleList.stream()
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            int count = vauleList.size();
            BigDecimal divide = sum.divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            result.setAgv(divide);
            result.setTotal(sum);
        }
        return result;
    }

    private static void handleMaxData(List<IotCubeElectricityConsumptionHistory> maxList, List<BigDecimal> vauleList, Integer type) {
        maxList.forEach(item -> {
            switch (type) {
                case 1:
                    if (item.getDailyTotal() != null) {
                        vauleList.add(item.getDailyTotal());
                    }
                    break;
                case 2:
                    if (item.getDailyAc1() != null) {
                        vauleList.add(item.getDailyAc1());
                    }
                    break;
                case 3:
                    if (item.getDailyAc2() != null) {
                        vauleList.add(item.getDailyAc2());
                    }
                    break;
                case 4:
                    if (item.getDailyLc1() != null) {
                        vauleList.add(item.getDailyLc1());
                    }
                    break;
                case 5:
                    if (item.getDailyLc2() != null) {
                        vauleList.add(item.getDailyLc2());
                    }
                    break;
                default:
                    if (item.getDailyTotal() != null) {
                        vauleList.add(item.getDailyTotal());
                    }
                    break;
            }
        });
    }

    private static void handleData(List<EnergyDTO> list, List<IotCubeElectricityConsumptionHistory> resultList, Integer type) {
        resultList.forEach(item -> {
            EnergyDTO energyDTO = new EnergyDTO();
            energyDTO.setTime(item.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
            switch (type) {
                case 1:
                    energyDTO.setData(item.getDailyTotal());
                    break;
                case 2:
                    energyDTO.setData(item.getDailyAc1());
                    break;
                case 3:
                    energyDTO.setData(item.getDailyAc2());
                    break;
                case 4:
                    energyDTO.setData(item.getDailyLc1());
                    break;
                case 5:
                    energyDTO.setData(item.getDailyLc2());
                    break;
                default:
                    energyDTO.setData(item.getDailyTotal());
                    break;
            }

            list.add(energyDTO);
        });
    }
}
